package com.yonyou.pmclouds.basecom.util;

import java.text.Collator;
import java.util.Locale;

public class ChineseStringUtils {
    private static ThreadLocal<Collator> chineseCollator = new ThreadLocal<Collator>() {
        @Override
        protected Collator initialValue() {
            return Collator.getInstance(Locale.CHINA);
        }
    };

    private ChineseStringUtils() {

    }

    public static int compareInDict(String s1, String s2, boolean nullLess) {
        if (s1 == s2) {
            return 0;
        }
        if (s1 == null) {
            if (nullLess) {
                return -1;
            } else {
                return 1;
            }
        }
        if (s2 == null) {
            if (nullLess) {
                return 1;
            } else {
                return -1;
            }
        }
        return chineseCollator.get().compare(s1, s2);
    }

    /**
     * 按字典序比较中文字符串的大小，默认空值较大
     */
    public static int compareInDict(String s1, String s2) {
        return compareInDict(s1, s2, false);
    }
}
